#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;

int n;
struct bz {
    int val;
    int room;
};

unordered_map<int, long long> roomVal;

int main() {
    cin >> n;
    bz bzs[n];
    for (int i = 0; i < n; i++) cin >> bzs[i].room;
    for (int i = 0; i < n; i++) cin >> bzs[i].val;
    sort(bzs, bzs + n, [](bz a, bz b) { return a.room >= b.room; });
    int maxRoom = bzs[0].room;
    for (int i = 1; i <= maxRoom; i++) { roomVal[i] = 0; }
    for (int i = 0; i < n; i++) {
        roomVal[bzs[i].room] += bzs[i].val;
    }
    for(int i=maxRoom;i>=1;i--){
        long long add = 0;
        if(i*2 <= maxRoom) add = max(add, roomVal[i*2]);
        if(i*2 + 1 <= maxRoom) add = max(add, roomVal[i*2+1]);
        roomVal[i] += add;
    }


    cout << roomVal[1] << endl;
    return 0;
}